<script type="text/javascript">
    var root = "__ROOT__";
    var app = "__APP__";
</script>
<script src="__PUBLIC__/Admin/Js/jquery.js"></script>
<script src="__PUBLIC__/static/layui/layui.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/js/dialog/layer.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/js/dialog.js"></script>
<script type="text/javascript" src="__PUBLIC__/static/js/fun.js"></script>
<script>
    //JavaScript代码区域
    layui.use('element', function(){
        var element = layui.element;
        element.on("nav(demo)",function (ele) {
            var title = ele.find("a").text();
            var url = ele.find("a").attr("_href");
            var menu_id = ele.find("a").attr("name");
            menuClick(element,title,url,menu_id);
        });

        element.on('tabDelete(title)', function(data){
            $('[lay-filter=title]').removeAttr("lay-allowClose");
            $('.layui-tab-title li').eq(0).find('i').remove();
        });

        //tab选项卡切换时侧边栏自动切换到对应位置
        element.on('tab(title)',function (data) {
            var index = data.index;//得到当前Tab的所在下标
            var src = $(".dy-iframe").eq(index).attr("src");
            var obj = $("[lay-filter=demo]").find("a[_href='"+src+"']");
            var menu_id = obj.attr("name");
            window.location.hash = menu_id;
            menuChange(obj);
        });

        //去到上次点击的菜单
        var menu_id = window.location.hash.split("#")[1];
        if(menu_id){
            var a_obj = $(".layui-nav-tree[lay-filter='demo']").find("a[name='"+menu_id+"']");
            a_obj[0].click();
            menuChange(a_obj);
        }

        //导航的hover效果、二级菜单等功能，需要依赖element模块
        // 侧边栏点击隐藏兄弟元素
        $('.layui-nav-item').click(function(event) {
            $(this).siblings().removeClass('layui-nav-itemed');
        });
    });

    /**
     * 菜单切换效果
     * @param obj
     */
    function menuChange(obj) {
        $('.layui-nav-item').removeClass('layui-nav-itemed');
        $('.layui-nav-item').removeClass('layui-this');
        $('.layui-nav-item a').removeClass('layui-this');
        $('.layui-nav-item dd').removeClass('layui-this');
        obj.addClass("layui-this");
        obj.parent().addClass('layui-this');
        if(!obj.parent().is("li")){
            obj.parent().parent().parent().addClass('layui-nav-itemed');
        }
    }

    /**
     * 菜单点击效果
     * @param title
     * @param url
     * @param menu_id
     */
    function menuClick(element,title,url,menu_id) {
        //地址已经打开直接切换选项卡
        for (var i = 0;i<$(".dy-iframe").length;i++){
            var dy_src = $(".dy-iframe").eq(i).attr("src");
            if(dy_src.toLowerCase() == url.toLowerCase()){
                element.tabChange("title",i+1);
                return ;
            }
        }
        //地址为打开
        //设置地址栏
        window.location.hash = menu_id;
        var len = $('.layui-tab-title li').length;
        if(len>=1){
            $('[lay-filter=title]').attr("lay-allowClose",true);
        }
        var res = element.tabAdd("title",{
            title:title,
            content:'<iframe frameborder="0" src="'+url+'" class="dy-iframe"></iframe>',
            id:$('.layui-tab-title li').length+1
        });
        element.tabChange('title', $('.layui-tab-title li').length);
        $('.layui-tab-title li').eq(0).find('i').remove();
    }
</script>